package cn.tedu.test.luban.rpc.client;

import cn.tedu.test.luban.rpc.HelloService;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class HelloServiceProxy implements HelloService {
    @Override
    public String sayHi(String name) {
        try{
            //创建socket 发送到localhost:20000
            Socket socket=new Socket("localhost",20000);
            //socket 在客户端向服务端发送数据使用outputstream 携带的是字符串的序列化二进制
            OutputStream outputStream = socket.getOutputStream();
            //包装给一个高级流
            DataOutputStream dos=new DataOutputStream(outputStream);
            dos.writeUTF(name);
            System.out.println("客户端传输序列化数据:"+name);
            //代理完成服务端返回数据的反序列化
            DataInputStream dis=new DataInputStream(socket.getInputStream());
            return dis.readUTF();
        }catch (Exception e){
            return null;
        }
    }
}
